#include "precompiled.h"
#pragma hdrstop

namespace inf {
    Random Random::staticRandom;

    Vec3 Random::random_vector_in_clone( float halfOpeningAngle ) {
        // This should give uniform distribution: http://www.flipcode.org/cgi-bin/fcmsg.cgi?thread_show=7028

        // Random height between cos(hO), 1
        float c = math::cos( math::DEG2RAD( halfOpeningAngle ) );
        float z = c + ( 1.0f - c ) * random_float();

        // Random between 0, 2 PI
        float a = random_float() * ( 2.0f * math::PI );

        // Make sure vector is normalized & apply azimuth
        float r = math::sqrt( 1.0f - z*z );
        float sin, cos; math::sincos( a, sin, cos );
        return Vec3( r * cos, r * sin, z );
    }

    Vec3 Random::random_vector_in_clone( Vec3 dir, float halfOpeningAngle ) {

        // Random height between cos(hO), 1
        float c = math::cos( math::DEG2RAD( halfOpeningAngle ) );
        float x = c + ( 1.0f - c ) * random_float();

        // Random between 0, 2 PI
        float a = random_float() * ( 2.0f * math::PI );

        // Make sure vector is normalized & apply azimuth
        float r = math::sqrt( 1.0f - x*x );
        float sin, cos; math::sincos( a, sin, cos );
        Vec3 result( x, r * cos, r * sin );

        return result * dir.to_Mat3();
    }

}   // ::inf
